package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.ReserveMapper;
import com.hqyj.dao.SysTableMapper;
import com.hqyj.dao.UserMapper;
import com.hqyj.pojo.Reserve;
import com.hqyj.pojo.SysTable;
import com.hqyj.pojo.User;
import com.hqyj.service.TableService;
import com.hqyj.utile.ColorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

@Service
public class TableServiceImpl implements TableService {
    @Autowired//直接去容器里面拿
    private SysTableMapper mapper;
    @Autowired
    private ReserveMapper reserveMapper;
    @Autowired
    private UserMapper userMapper;

    Reserve reserve = new Reserve();
    User user = new User();


    public HashMap<String, Object> showTableList(SysTable table) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //模糊查询判断,传入参数是想要去做模糊查询
        if(table.gettName()!=null){
            table.settName("%"+table.gettName()+"%");
        }
        if(table.gettState()!=null){
            table.settState("%"+table.gettState()+"%");
        }

        //分页
        //1 设置分页查询的页码，每页显示的行数
        PageHelper.startPage(table.getPage(), table.getRow());
        //2 查询
        System.out.println(table);

        System.out.println("================="+table.gettId());
        List<SysTable> list = mapper.selectAllTables(table);
        List<SysTable> listColor = ColorUtil.updateTableColor(list);
       // System.out.println("===================颜色是啥啊？"+listColor.get(0).getColName());
        PageInfo<SysTable> pageInfo = new PageInfo<SysTable>(listColor);

        if(list.size()==0){
            map.put("info","没有查询到数据");
        }else{
            //获取当前页的集合
            map.put("info",pageInfo.getList());
            //总条数
            Long  total = pageInfo.getTotal();
            //总页数
            int  totalPage  = pageInfo.getPages();
            System.out.println("上一页="+pageInfo.getPrePage());
            System.out.println("下一页="+pageInfo.getNextPage());
            map.put("total",total);
            map.put("totalPage",totalPage);
//            map.put("style1","style='background-color: #33bbdd'");
        }

        return map;
    }

    public HashMap<String, Object> tableUpdate(SysTable table) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //模糊查询判断
        //获取时间、设置时间
        System.out.println("当前的id值为："+table.gettId());
        Calendar dateNow = Calendar.getInstance();
        Calendar dateAfter = Calendar.getInstance();
        dateAfter.add(Calendar.HOUR, 2);

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        String dateStrNow = sdf.format(dateNow.getTime());
        String dateStrAfter = sdf.format(dateAfter.getTime());
        System.out.println(dateStrNow+"    "+dateStrAfter);
        //获取前端的----上、中下
        System.out.println("当前的value值为："+table.gettWhen());
        table.settState("已预定、待审核");
        table.settTime(dateStrNow);
        table.settArrtime(dateStrAfter);
        int success = mapper.updateByPrimaryKey(table);
        System.out.println("==================="+success+"================");
        map.put("info",success);



        //分页
        //1 设置分页查询的页码，每页显示的行数
        PageHelper.startPage(table.getPage(), table.getRow());
        //2 查询
        System.out.println(table);
        List<SysTable> list = mapper.selectAllTables(table);
//        for (int i = 0; i < list.size(); i++) {
//            String s = DateUtil.formatDate(list.get(i).gettArrtime(), "yyyy-MM-dd HH:mm:ss");
//            map.put("date",s);
//        }

        //3 获取分页对象
        PageInfo<SysTable> pageInfo = new PageInfo<SysTable>(list);

        if(list.size()==0){
            map.put("info","没有查询到数据");
        }else{
            //获取当前页的集合
            map.put("info",pageInfo.getList());
            //总条数
            Long  total = pageInfo.getTotal();
            //总页数
            int  totalPage  = pageInfo.getPages();
            System.out.println("上一页="+pageInfo.getPrePage());
            System.out.println("下一页="+pageInfo.getNextPage());
            map.put("total",total);
            map.put("totalPage",totalPage);
        }

        return map;
    }

    public HashMap<String, Object> Update(SysTable table) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        //模糊查询判断
        //获取时间、设置时间
        System.out.println("当前的id值为："+table.gettId());
        //获取前端的----上、中下
        table.settState("可抢座");

        System.out.println("=========================="+table.gettTime()+"==================");
        if(table.gettTime()!=""){
            table.settTime(table.gettTime());
            table.settArrtime(table.gettArrtime());
            table.settUserName(table.gettUserName());
            table.settUsertel(table.gettUsertel());
            //下单后在reserve里面插入一条待处理的数据
            int tUserId = userMapper.selectUserIdByName(table.gettUserName());
            System.out.println("========这个时候的ID为："+tUserId+"==============");
            reserve.settId(table.gettId());
            reserve.setUserId(tUserId);
            reserve.settArrtime(table.gettArrtime());
            reserve.settTime(table.gettTime());
            System.out.println("]=====================["+table.getUserNumber());
            reserve.setUserNumber(table.getUserNumber());
            int insertSuccess = reserveMapper.insert(reserve);
            int success = mapper.updateByPrimaryKey(table);
            if (success == 1){
                System.out.println("==================="+success+"================");
                map.put("info","申请成功，待审核！");
                return map;
            }else{
                map.put("info","GG");
                return map;
            }
        }else{
                map.put("info","请输入你所想要选择的时间段！！");
                return map;

        }
    }


}
